/*
*	Project: Aethon
*
*	File: Eye.cpp
*
*	Author: Jonathan Hersack
*
*	Date: 2/15/2009
*
*   Purpose: The Eye contains the look at information for the viewpoint and 
*   the wireframe and activity modifier flags.
*/

#include "../include/Eye.h"

void Eye::update(vector3f modelPosition, vector3f modelDirection)
{
if(getIsActive())
{
	vector3f tempvect = calcPoints(modelDirection);
	tempvect.v[0]+=modelPosition.v[0];//new look = x point + x position
	tempvect.v[1]+=modelPosition.v[1];
	tempvect.v[2]+=modelPosition.v[2];

	setLookAt(tempvect);
	system("cls");
	cout<<"Rotx: "<<modelDirection.v[0]<< endl;
	cout<<"RotY: "<<modelDirection.v[1]<< endl<< endl;
	cout<<"lookx: "<<getLookAt().v[0]<< endl;
	cout<<"looky: "<<getLookAt().v[1]<< endl;
	cout<<"lookz: "<<getLookAt().v[2]<<endl<< endl;
	
	//call update of camera look using newly created lookat point
	gluLookAt(modelPosition.v[0],modelPosition.v[1],modelPosition.v[2],
	getLookAt().v[0],getLookAt().v[1],getLookAt().v[2],0,1,0);
}
}//end update


//############################################################
vector3f Eye::calcPoints(vector3f angles)
{
 vector3f newLook;
 
/*
float xPoint=cos(angles.v[1]*PI/180.0)*cos(angles.v[0]*PI/180.0);
float yPoint=cos(angles.v[1]*PI/180.0)*sin(angles.v[0]*PI/180.0); 
float zPoint=sin(angles.v[1]*PI/180.0);
*//*
http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial26.php
Rotation matrix
[R] = | cos(yRot)   sin(xRot)*sin(yRot)  -cos(xRot)*sin(yRot) |
      | 0           cos(xRot)             sin(xRot)           |
      | sin(yRot)  -sin(xRot)*cos(yRot)   cos(xRot)*cos(yRot) | 
	  
	  yrotmatrix*xrotmatrix
*/


 float xPoint = cos(angles.v[1]*PI/180.0) + sin(angles.v[0]*PI/180.0)*sin(angles.v[1]*PI/180.0) - cos(angles.v[0]*PI/180.0)*sin(angles.v[1]*PI/180.0);
 float yPoint = 0 +  cos(angles.v[0]*PI/180.0)+sin(angles.v[0]*PI/180.0);
 float zPoint = sin(angles.v[1]*PI/180.0) - sin(angles.v[0]*PI/180.0) * cos(angles.v[1]*PI/180.0) + cos(angles.v[0]*PI/180.0)*cos(angles.v[1]*PI/180.0);
 
 newLook.v[0]=xPoint;
 newLook.v[1]=yPoint;
 newLook.v[2]=zPoint;
 
 newLook = normalizeVector(newLook);
 
  return newLook;
 }//end calcFpointZ

 //######################################################################3
vector3f Eye::normalizeVector(vector3f vect)
{
	float magnitude = sqrt(vect.v[0]*vect.v[0]+vect.v[1]*vect.v[1]+vect.v[2]*vect.v[2]);
	vector3f unitVect;
	unitVect.v[0]=vect.v[0]/magnitude;
	unitVect.v[1]=vect.v[1]/magnitude;
	unitVect.v[2]=vect.v[2]/magnitude;
	return unitVect;
}//end normailzevector